Skip to content

fix(patch): prevent invalid fuzzy match ranges#2686

Merged
amitksingh1490 merged 5 commits intomainfrom
pr/bug-458df055a
Mar 25, 2026
Merged

fix(patch): prevent invalid fuzzy match ranges#2686
amitksingh1490 merged 5 commits intomainfrom
pr/bug-458df055a

Conversation

@amitksingh1490
Copy link
Contributor

Summary

Fix incorrect range computation for fuzzy search matches in fs_patch and add bounds checking to prevent out-of-range replacements.

Context

Range::from_search_match previously derived byte offsets by assuming a single line-ending style across the entire file. That could produce incorrect ranges for content with mixed line endings, and downstream replacement logic could then attempt to index outside the source buffer.

Changes

  • Reworked fuzzy-match range calculation to scan the source and use actual byte positions for each line
  • Clamped start and end line indices to the valid file bounds
  • Added an explicit RangeOutOfBounds error before slicing replacement ranges
  • Added regression coverage for mixed line endings, trailing newlines, unicode content, and out-of-bounds matches

Key Implementation Details

The fix is centered in crates/forge_services/src/tool_services/fs_patch.rs, where range creation now tracks exact line start/end byte offsets instead of inferring offsets from a detected newline length. Replacement now validates the computed range before indexing into the source string.

Testing

cargo test -p forge_services fs_patch

@tusharmath tusharmath changed the title fix(fs_patch): prevent invalid fuzzy match ranges fix(patch): prevent invalid fuzzy match ranges Mar 25, 2026
@amitksingh1490 amitksingh1490 merged commit 249ec9b into main Mar 25, 2026
15 of 16 checks passed
@amitksingh1490 amitksingh1490 deleted the pr/bug-458df055a branch March 25, 2026 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant